﻿using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Resources;
using Lightray.Hattrick.ChppLib;
using Lightray.Hattrick.ChppLib.EntityTypes;

namespace Lightray.Hattrick
{
    public class ResourceHandler
    {
        private static Dictionary<Country, Image> _flagImages;

        static ResourceHandler()
        {
            loadCountryFlagImages();
        }

        public static Image CountryFlag(Country countries)
        {
            if (_flagImages.ContainsKey(countries))
                return _flagImages[countries];
            else
                return null;
        }

        public static Image ImageForCards(int cards)
        {
            switch (cards)
            {
                case 1:
                    return Properties.Resources.yellowcard;
                case 2:
                    return Properties.Resources.yellowcards;
                case 3:
                    return Properties.Resources.redcard;
                default:
                    return null;
            }
        }

        private static void loadCountryFlagImages()
        {
            Stream flagResourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Lightray.Hattrick.Properties.Flags.resources");
            IResourceReader resourceReader = new ResourceReader(flagResourceStream);
            _flagImages = new Dictionary<Country, Image>();

            foreach (DictionaryEntry entry in resourceReader)
            {
                try
                {
                    string isoCountryCode = entry.Key.ToString();
                    if (!Utility.HasIsoCodeListed(isoCountryCode))
                        continue;
                        
                    Country key = Utility.Iso3166AsCountry(isoCountryCode);
                    Image value = entry.Value as Bitmap;
                    _flagImages[key] = value;
                }
                catch
                {
                    continue;
                }
            }
        }
    }
}
